home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / rend10.lzh / REND1.0 / GraphicSubSystem / transformation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  3.8 KB  |  171 lines

  1. /*
  2.  * Title:
  3.  *    transformation.c
  4.  *
  5.  * Authors:
  6.  *    Michael P. Schenck 
  7.  *
  8.  * Purpose:
  9.  *    These routines are used for constructing transformation matricies.  They 
  10.  *    all take a variety of rotation, scaling, and translation parameters.
  11.  *     They include scaling-rotation-translation (srt), scaling-translation-rotation
  12.  *    (str), x-rotation, y-rotation, z-rotation, scaling, and translating.
  13.  *
  14.  * Copyright Info:
  15.  *    Copyright (C) 1993, 1994 -- by Michael P. Schenck, 
  16.  *    (mps4466@ultb.isc.rit.edu)
  17.  *    
  18.  *    This program is free software; you can redistribute it and/or modify
  19.  *    it under the terms of the GNU General Public License as published
  20.  *    by the Free Software Foundation; either version 2 of the License,
  21.  *    or (at your option) any later version.
  22.  *
  23.  *    This software is distributed in the hope that it will be useful, but
  24.  *    WITHOUT ANY WARRANTY; without even the implied warranty of
  25.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26.  *    GNU General Public License for more details.
  27.  *
  28.  *      For a copy of the GNU General Public License
  29.  *    write to the Free Software Foundation, 675 Mass Ave,
  30.  *    Cambridge, MA  02139, USA.
  31.  *
  32.  */
  33.  
  34. #include <stdlib.h>
  35. #include <math.h>
  36. #include "/include/types.h"
  37. #include "/include/errors.h"
  38. #include "/include/matrix.h"
  39. #include "/include/transformation.h"
  40.  
  41. static ULONG i,j;
  42. static MATRIX m[MAXMATREQ];
  43. static FLOAT sintheta,costheta;
  44.  
  45.     /* Open and allocate needed matricies. */
  46.  
  47. int opentransformation()
  48.         
  49. {
  50.    for(i=0;i<MAXMATREQ;i++) {
  51.       if((m[i] = allocatematrix()) == NULL) {
  52.      for(j=0;j<i;j++)
  53.         freematrix(m[j]);
  54.      return(MEM_ALLOC_FAILURE);
  55.       }
  56.    }
  57.    return(SUCCESS);
  58. }
  59.  
  60.     /* Sets a scale, rotation, and translation matrix. */
  61.       
  62. void setsrttrans(FLOAT sx,FLOAT sy,FLOAT sz,
  63.          FLOAT rx,FLOAT ry,FLOAT rz,
  64.          FLOAT dx,FLOAT dy,FLOAT dz,
  65.          MATRIX rm)
  66.  
  67. {
  68.    setscalematrix(sx,sy,sz,m[0]);
  69.    setrotxmatrix(rx,m[1]);
  70.    setrotymatrix(ry,m[2]);
  71.    setrotzmatrix(rz,m[3]);
  72.    settransmatrix(dx,dy,dz,m[4]);
  73.    multmatrix(m[0],m[1],m[5]);
  74.    multmatrix(m[5],m[2],m[6]);
  75.    multmatrix(m[6],m[3],m[5]);
  76.    multmatrix(m[5],m[4],rm);
  77. }
  78.  
  79.     /* Sets a matrix same as above, but translation is done before rotation.
  80.        Very helpful for objects that need to be rotated to a position 
  81.        not on an axis. */
  82.  
  83. void setstrtrans(FLOAT sx,FLOAT sy,FLOAT sz,
  84.          FLOAT dx,FLOAT dy,FLOAT dz,
  85.          FLOAT rx,FLOAT ry,FLOAT rz,
  86.          MATRIX rm)
  87.  
  88. {
  89.    setscalematrix(sx,sy,sz,m[0]);
  90.    settransmatrix(dx,dy,dz,m[1]);
  91.    setrotxmatrix(rx,m[2]);
  92.    setrotymatrix(ry,m[3]);
  93.    setrotzmatrix(rz,m[4]);
  94.    multmatrix(m[0],m[1],m[5]);
  95.    multmatrix(m[5],m[2],m[6]);
  96.    multmatrix(m[6],m[3],m[5]);
  97.    multmatrix(m[5],m[4],rm);
  98. }
  99.  
  100.     /* X-axis rotation matrix. */
  101.  
  102. void setrotxmatrix(FLOAT theta,MATRIX rm)
  103.  
  104. {
  105.    sintheta = sin(theta);
  106.    costheta = cos(theta);
  107.    identitymatrix(rm);
  108.    *(rm+5) = costheta;
  109.    *(rm+6) = sintheta;
  110.    *(rm+9) = -sintheta;
  111.    *(rm+10) = costheta;
  112. }
  113.  
  114.     /* Y-axis rotation matrix. */
  115.  
  116. void setrotymatrix(FLOAT theta,MATRIX rm)
  117.  
  118. {
  119.    sintheta = sin(theta);
  120.    costheta = cos(theta);
  121.    identitymatrix(rm);
  122.    *rm = costheta;
  123.    *(rm+2) = -sintheta;
  124.    *(rm+8) = sintheta;
  125.    *(rm+10) = costheta;
  126. }
  127.  
  128.     /* Z-axis rotation matrix. */
  129.  
  130. void setrotzmatrix(FLOAT theta,MATRIX rm)
  131.  
  132. {
  133.    sintheta = sin(theta);
  134.    costheta = cos(theta);
  135.    identitymatrix(rm);
  136.    *rm = costheta;
  137.    *(rm+1) = sintheta;
  138.    *(rm+4) = -sintheta;
  139.    *(rm+5) = costheta;
  140. }
  141.  
  142.     /* Traslation matrix. */
  143.  
  144. void settransmatrix(FLOAT dx,FLOAT dy,FLOAT dz,MATRIX rm)
  145.  
  146. {
  147.    identitymatrix(rm);
  148.    *(rm+12) = dx;
  149.    *(rm+13) = dy;
  150.    *(rm+14) = dz;
  151. }
  152.  
  153.     /* Scaling matrix. */
  154.  
  155. void setscalematrix(FLOAT sx,FLOAT sy,FLOAT sz,MATRIX rm)
  156.  
  157. {
  158.    identitymatrix(rm);
  159.    *rm = sx;
  160.    *(rm+5) = sy;
  161.    *(rm+10) = sz;
  162. }
  163.  
  164.     /* Closeup, releasing matricies. */
  165.  
  166. void closetransformation()
  167.  
  168. {
  169.    for(i=0;i<MAXMATREQ;i++)
  170.       freematrix(m[i]);
  171. }